<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/xlsx/0.11.5/xlsx.core.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
</head>

<body>
  <input id="file" type="file">
</body>

</html>
<script>
  const dom = document.getElementById('file');

  const transKeyNameList = ['zh', 'en'];

  dom.addEventListener('change', (e) => {
    let obj = {};
    importExcel(e.target.files[0]).then((res) => {
      const sheetList = Object.keys(res);
      sheetList.forEach((item) => {
        res[item].forEach((item2) => {
          transKeyNameList.forEach((item3) => {
            if (!obj[item3]) {
              obj[item3] = {};
            }
            obj[item3][item2.key] = item2[item3] || '';
          })
        });
      })
      Object.keys(obj).forEach((item) => {
        const blob = new Blob([JSON.stringify(obj[item])], {
          type: "text/plain;charset=utf-8"
        });
        saveAs(blob, `${item}.json`);
      })
      // dom.outerHTML = dom.outerHTML;
    });
  })

  /** 导入Excel */
  function importExcel(file) {
    const that = this;
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.onload = function (e) {
        const data = e.target.result;
        const workbook = XLSX.read(data, {
          type: 'binary'
        });
        if (workbook) {
          const workbookObj = that.readWorkbook(workbook);
          resolve(workbookObj);
        } else {
          return reject('excel读取失败');
        }
      };
      // 读取本地excel文件
      reader.readAsBinaryString(file);
    });
  }

  /** 读取工作簿 */
  function readWorkbook(workbook) {
    const sheetNames = workbook.SheetNames || []; // 工作表名称集合
    const sheetObj = {};
    sheetNames.forEach((sheetName) => {
      const worksheet = workbook.Sheets[sheetName];
      sheetObj[sheetName] = XLSX.utils.sheet_to_json(worksheet);
    })
    return sheetObj;
  }
</script>